package com.kedacom.freedomLabel.viewmodel.product;


import com.kedacom.freedomLabel.Options;
import com.kedacom.freedomLabel.base.BaseViewModel;
import com.kedacom.freedomLabel.model.DataControlResult;
import com.kedacom.freedomLabel.model.bean.Operator;
import com.kedacom.freedomLabel.model.bean.Product;
import com.kedacom.freedomLabel.model.db.AppDatabase;
import com.kedacom.freedomLabel.model.db.dao.OperatorDao;
import com.kedacom.freedomLabel.model.db.dao.ProductDao;
import com.kedacom.freedomLabel.utils.Cn2Spell;
import com.kedacom.lego.fast.validation.RegexTemplate;
import com.kedacom.lego.message.LegoMessageManager;
import com.kedacom.lego.mvvm.ObservableLiveDataField;

import java.util.List;
import java.util.regex.Pattern;

public class ProductControlViewModel extends BaseViewModel {

    public ObservableLiveDataField<String> evNumberText = new ObservableLiveDataField<>();
    public ObservableLiveDataField<String> evNameText = new ObservableLiveDataField<>();
    public ObservableLiveDataField<Boolean> boxGroupChecked = new ObservableLiveDataField<>(false);
    public ObservableLiveDataField<String> tvGroupText = new ObservableLiveDataField<>();
    public ObservableLiveDataField<String> evSingleWeightText = new ObservableLiveDataField<>();
    public ObservableLiveDataField<String> evPreTareText = new ObservableLiveDataField<>();
    public ObservableLiveDataField<String> evTopText = new ObservableLiveDataField<>();
    public ObservableLiveDataField<String> evLowText = new ObservableLiveDataField<>();
    public ObservableLiveDataField<String> tvLabelText = new ObservableLiveDataField<>();
    public ObservableLiveDataField<Boolean> tvIdChecked = new ObservableLiveDataField<>(false);

    public void saveID(String groupTag, String labelTag, String groupNumber) {
        Product item = new Product();
        item.groupUUID = groupTag;
        item.groupNumber = groupNumber;
        item.label = labelTag;
        item.groupName = tvGroupText.getValue();
        item.number = evNumberText.getValue();
        item.name = evNameText.getValue();
        item.shortHand = Cn2Spell.getPinYinHeadChar(item.name);
        item.openGroupSetting = boxGroupChecked.getValue() == null ? false : boxGroupChecked.getValue();
        item.openPreIDSetting = tvIdChecked.getValue() == null ? false : tvIdChecked.getValue();
        item.createTime = System.currentTimeMillis();
        String singleWeightTextValue = evSingleWeightText.getValue();


        if (isCheckTextBoolean(singleWeightTextValue)) {
            item.singleWeight = Double.parseDouble(singleWeightTextValue);
        } else {
            item.singleWeight = Double.parseDouble("0.000");
        }
        String tareTextValue = evPreTareText.getValue();
        if (isCheckTextBoolean(tareTextValue)) {
            item.preTare = Double.parseDouble(tareTextValue);
        } else {
            item.preTare = Double.parseDouble("0.000");
        }
        String topTextValue = evTopText.getValue();
        if (isCheckTextBoolean(topTextValue)) {
            item.topLimit = Double.parseDouble(topTextValue);
        } else {
            item.topLimit = Double.parseDouble("0.000");
        }
        String lowTextValue = evLowText.getValue();
        if (isCheckTextBoolean(lowTextValue)) {
            item.lowLimit = Double.parseDouble(lowTextValue);
        } else {
            item.lowLimit = Double.parseDouble("0.000");
        }

        ProductDao dao = AppDatabase.getInstance().productDao();
        List<Product> products = dao.get(evNumberText.getValue());

        if(products.size()>0){
            DataControlResult result = new DataControlResult<>(products.get(0));
            LegoMessageManager.getInstance().sendMessage(Options.MSG_ADD_EXISTS_RESULT, result);
        }else {
            LegoMessageManager.getInstance().sendMessage(Options.MSG_ADD_ITEM, item);
        }


//        LegoMessageManager.getInstance().sendMessage(Options.MSG_ADD_ITEM, item);
    }

    private boolean isCheckTextBoolean(String topTextValue) {
        return topTextValue != null && Pattern.compile(RegexTemplate.NOT_EMPTY).matcher(topTextValue).matches()
                && (Pattern.compile(RegexTemplate.POSITIVE_FLOAT).matcher(topTextValue).matches()
                || Pattern.compile(RegexTemplate.POSITIVE_INTEGER).matcher(topTextValue).matches());
    }

    public void updateID(Product item) {
        String singleWeightTextValue = evSingleWeightText.getValue();
        if (Pattern.compile(RegexTemplate.NOT_EMPTY).matcher(singleWeightTextValue).matches()
                && (Pattern.compile(RegexTemplate.POSITIVE_FLOAT).matcher(singleWeightTextValue).matches())
                || Pattern.compile(RegexTemplate.POSITIVE_INTEGER).matcher(singleWeightTextValue).matches()) {
            item.singleWeight = Double.parseDouble(singleWeightTextValue);
        }
        String tareTextValue = evPreTareText.getValue();
        if (Pattern.compile(RegexTemplate.NOT_EMPTY).matcher(tareTextValue).matches()
                && (Pattern.compile(RegexTemplate.POSITIVE_FLOAT).matcher(tareTextValue).matches())
                || Pattern.compile(RegexTemplate.POSITIVE_INTEGER).matcher(tareTextValue).matches()) {
            item.preTare = Double.parseDouble(tareTextValue);
        } else {
            item.preTare = Double.parseDouble("0.000");
        }
        String topTextValue = evTopText.getValue();
        if (Pattern.compile(RegexTemplate.NOT_EMPTY).matcher(topTextValue).matches()
                && (Pattern.compile(RegexTemplate.POSITIVE_FLOAT).matcher(topTextValue).matches())
                || Pattern.compile(RegexTemplate.POSITIVE_INTEGER).matcher(topTextValue).matches()) {
            item.topLimit = Double.parseDouble(topTextValue);
        } else {
            item.topLimit = Double.parseDouble("0.000");
        }
        String lowTextValue = evLowText.getValue();
        if (Pattern.compile(RegexTemplate.NOT_EMPTY).matcher(lowTextValue).matches()
                && (Pattern.compile(RegexTemplate.POSITIVE_FLOAT).matcher(lowTextValue).matches())
                || Pattern.compile(RegexTemplate.POSITIVE_INTEGER).matcher(lowTextValue).matches()) {
            item.lowLimit = Double.parseDouble(lowTextValue);
        } else {
            item.lowLimit = Double.parseDouble("0.000");
        }
        item.groupName = tvGroupText.getValue();
        item.number = evNumberText.getValue();
        item.name = evNameText.getValue();
        item.shortHand = Cn2Spell.getPinYinHeadChar(item.name);
        item.openGroupSetting = boxGroupChecked.getValue() == null ? false : boxGroupChecked.getValue();
        item.openPreIDSetting = tvIdChecked.getValue() == null ? false : tvIdChecked.getValue();
        item.updateTime = System.currentTimeMillis();
        LegoMessageManager.getInstance().sendMessage(Options.MSG_SAVE_ITEM, item);
    }
}